
/*
 * @author : TF-BJ-C064
 * @creation : 2014-6-26 下午9:19:22
 * @description : 
 *
 */

package com.service.impl;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.base.PageBean;
import com.base.UserBean;
import com.bean.ParkManager;
import com.bean.ParkOrder;
import com.bean.Parkinfo;
import com.bean.PerformanceBean;
import com.bean.UserCreditRecord;
import com.bean.Userinfo;
import com.dao.ParkOrderDao;

public class ParkOrderServiceImpl implements com.service.ParkOrderService {

	ParkOrderDao dao;

	@Override
	public List<Object> query(Object object) throws Exception {
		return dao.getAll((ParkOrder) object);
	}
	@Override
	public List<ParkOrder> query(ParkOrder object, UserBean userBean,PageBean page)
			throws Exception {
		return dao.findAll(page);
	}

	@Override
	public String add(Object object) throws Exception {
		return null;
	}

	@Override
	public String update(Object order) throws Exception {
		return dao.update((ParkOrder)order);
	}
	//停车场管理员查询本停车场的所有订单
	@Override
	public List<ParkOrder> queryMyParkOrder(ParkOrder object, UserBean userBean,PageBean page) throws Exception {
		List<ParkOrder> list = dao.queryMyParkOrder((ParkOrder) object,userBean, page);
		if(list==null || list.size()==0)
			return null;
		return list;
	}

	//收费员查询自己的订单
	@Override
	public List<ParkOrder> queryMyOrder(ParkOrder object, UserBean userBean,PageBean page) throws Exception {
		return dao.queryMyOrder(object, userBean,page);
	}

	// ----- getter & setter ----

	public ParkOrderDao getDao() {
		return dao;
	}
	public void setDao(ParkOrderDao dao) {
		this.dao = dao;
	}

	public void delete(ParkOrder order) throws Exception {
		dao.delete(order);
	}

	@Override
	public List<ParkOrder> getOrderList(Long id) throws Exception {
		return dao.getOrderList(id);
	}

	@Override
	public void deleteOrder(ParkOrder order) {
		dao.delete(order);
	}

	@Override
	public Object get(Long id) {
		return dao.findById(id);
	}
	@Override
	public ParkOrder findByOrId(Long id) {
		return dao.findByOrId(id);
	}
	@Override
	public void updateOrder(ParkOrder order) {
		dao.update(order);
	}
	@Override
	public String delete(Integer id, Integer flag) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String delete(Long id, Integer flag) {
		return dao.delete(id, flag);
	}
	@Override
	public Object get(Integer id) throws Exception {
		return null;
	}
	@Override
	public String updateuserandrecord(Userinfo user, ParkOrder parkOrder) {
		try {
			/*
			 * 用户信用值记录
			 */
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			//创建时间
			UserCreditRecord record = new UserCreditRecord();
			record.setCreateTime(ts);
			Short type=2;
			Short status=1;
			//记录类型
			record.setType(type);
			//状态
			record.setStatus(status);
			//订单ID
			record.setParkOrder(parkOrder);
			//用户ID
			record.setUserinfo(user);
			//信用值
			record.setCredit(-1);

			dao.updateuserrecord(record);//add record

			/*
			 * 用户表
			 */
			user.setCredit(user.getCredit()-1);
			user.setScore(user.getScore()-1);
			user.setUpdateTime( ts);
			dao.updateuserinfo(user);
		} catch (Exception e) {
			e.getStackTrace();
		}
		return  null;
	}
	@Override
	public ParkOrder findByuserinfoId(Long id) {
		return dao.findByuserinfoId(id);
	}
	@Override
	public List<ParkOrder> queryParkOrder(ParkOrder object, UserBean userBean,PageBean page) {
		return dao.queryParkOrder(object, userBean,page);
	}
	@Override
	public List<ParkOrder> getPname(UserBean userBean) {
		return dao.findparkname(userBean);
	}
	@Override
	public List<ParkOrder> statistics(ParkOrder parkOrder,UserBean userBean,String datetime) {
		return dao.statistics(parkOrder,userBean,datetime);
	}
	@Override
	public ParkOrder queryDaynum(ParkOrder object, UserBean userBean) {
		return dao.queryDaynum(object, userBean);
	}
	@Override
	public List<ParkOrder> getstatus() {
		return dao.getstatus();
	}
	@Override
	public List getAll(ParkOrder parkOrder) {
		return dao.getAll(parkOrder);
	}
	@Override
	public String add(ParkManager parkManager) {
		return null;
	}

	/**
	 * 绩效统计
	 * <p>Title: statisMyParkPerformance</p>
	 * <p>Description: </p>
	 * @param pm
	 * @param parkInfo
	 * @param dateTime
	 * @param page
	 * @return
	 * @throws Exception
	 * @see com.service.ParkOrderService#statisMyParkPerformance(com.bean.ParkManager, com.bean.Parkinfo, java.lang.String, com.base.PageBean)
	 */
	@Override
	public List<PerformanceBean> statisMyParkPerformance(List<Parkinfo> listPark, ParkManager pm, Parkinfo parkInfo, String dateTime, PageBean page) throws Exception {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		try{
			Date time = sdf.parse(dateTime);
		}catch(Exception e){
			return null;
		}
		List<PerformanceBean> listPF = dao.statisMyParkPerformance(listPark, pm, parkInfo, dateTime, page);
		if(listPF!=null){
			Double sum = 0.0;
			for(PerformanceBean pfb : listPF){
				sum += pfb.getAcAmount();
			}
			if(sum!=null){
				for(PerformanceBean pfb : listPF){
					pfb.setPercent( (float)( ((int)(pfb.getAcAmount()*10000/sum + 0.5))/100));
				}
			}
			Collections.sort(listPF, new Comparator<PerformanceBean>() {
				@Override
				public int compare(PerformanceBean p1, PerformanceBean p2) {
					return (int) (p2.getPercent()-p1.getPercent());
				}
			});
		}
		return listPF;

	}
}
